package com.bootdo.app.controller;

import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.app.utils.returnresponse.ServerResponse;
import com.bootdo.appsystem.domain.ShipaddressDO;
import com.bootdo.appsystem.domain.UseraccountDO;
import com.bootdo.appsystem.service.ShipaddressService;
import com.bootdo.common.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 【@author 】 @fan
 * 【@Email  】 mooncluyed@outlook.com
 * 【类解释  】 地址相关代码
 * 【@Date   】 2018/5/13 0013
 **/
@Controller
@RequestMapping("/app/address/")
public class AppAdressController  extends BaseController {

    @Autowired
    private ShipaddressService shipaddressService;

    //地址列表页
    @GetMapping("tomyaddress")
    public String toMyAddress( Model model){
        UseraccountDO useraccountDO = (UseraccountDO) this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
        HashMap<String , Object> map = new HashMap<>();
        map.put("uid",useraccountDO.getUid());
        List<ShipaddressDO> list = shipaddressService.list(map);
        model.addAttribute("addressList",list);
        return "app/my/address/address";
    }
    //地址修改页面
    @GetMapping("toeditaddress/{shipid}")
    public String toEditMyAddress(@PathVariable("shipid")Integer shipid , Model model){
        ShipaddressDO shipaddress = shipaddressService.get(shipid);
        model.addAttribute("shipaddress",shipaddress);
        return "app/my/address/editAddress";
    }
    //地址添加页面
    @GetMapping("toaddaddress")
    public String toAddMyAddress(){
        return "app/my/address/addAddress";
    }

    //添加地址功能
    @PostMapping("msgAddAddress")
    @ResponseBody
    public ServerResponse msgAddAddress(ShipaddressDO shipaddressDO){
        UseraccountDO useraccountDO = (UseraccountDO)this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
        HashMap<String,Object> map = new HashMap<>();
        map.put("uid",useraccountDO.getUid());
        List<ShipaddressDO> list = shipaddressService.list(map);
        if (list.size() >= 5){
            return ServerResponse.createByErrorCodeMessage(100,"超过5个地址");
        }
        //将其他的是否为默认地址置为一
        shipaddressService.updateOtherDefault(useraccountDO.getUid());

        shipaddressDO.setUid(useraccountDO.getUid());
        int save = shipaddressService.save(shipaddressDO);
        if(save > 0) {
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }
    //修改地址功能
    @PostMapping("msgEditAddress")
    @ResponseBody
    public ServerResponse msgEditAddress(ShipaddressDO shipaddressDO) {
        if(shipaddressDO.getIsdefault().equals(1)) {
            UseraccountDO useraccountDO = (UseraccountDO)this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
            shipaddressService.updateOtherDefault(useraccountDO.getUid());
        }
        int update = shipaddressService.update(shipaddressDO);
        if (update > 0) {
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }

    //获取默认地址数据
    @PostMapping("getDefaultAddress")
    @ResponseBody
    public ServerResponse getDefaultAddress( ) {
        UseraccountDO useraccountDO = (UseraccountDO)this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
        ShipaddressDO userDefault = shipaddressService.getUserDefault(useraccountDO.getUid(), 1);
        return ServerResponse.createBySuccess(userDefault);
    }

    //获取默认地址数据
    @PostMapping("getAddressList")
    @ResponseBody
    public ServerResponse getAddressList( ) {
        UseraccountDO useraccountDO = (UseraccountDO) this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
        HashMap<String , Object> map = new HashMap<>();
        map.put("uid",useraccountDO.getUid());
        List<ShipaddressDO> list = shipaddressService.list(map);
        return ServerResponse.createBySuccess(list);
    }

    //获取address数据
    @PostMapping("getAddressInfo")
    @ResponseBody
    public ServerResponse getAddressInfo(){
        UseraccountDO useraccountDO = (UseraccountDO) this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
        ShipaddressDO userDefault = shipaddressService.getUserDefault(useraccountDO.getUid(), 1);
        HashMap<String , Object> map = new HashMap<>();
        map.put("uid",useraccountDO.getUid());
        List<ShipaddressDO> list = shipaddressService.list(map);
        HashMap<String , Object> result = new HashMap<>();
        result.put("userDefault",userDefault);
        result.put("list",list);
        return ServerResponse.createBySuccess(result);
    }



}


